package com.tskj.leonsu.service;

import com.alibaba.fastjson.JSONObject;
import com.tskj.leonsu.dao.StudentDAO;
import com.tskj.leonsu.utility.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentService {

    @Autowired
    private StudentDAO studentDAO;

    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveCheckStatus(String adUserName, JSONObject student) {
        JSONObject ret = new JSONObject();

        String studentNo = student.getString("no");
        String sex = student.getString("sex");
        String xueYuan = student.getString("xueyuan");
        try {
            studentDAO.saveCheckStatus(adUserName, studentNo, sex, xueYuan, 1);
            ret.put("code", 0);
        } catch (Exception e) {
            e.printStackTrace();
            ret.put("code", 1);
            ret.put("errMsg", "保存中出现错误，请联系管理员");
        }
        return ret;
    }

    /**
     * 检查状态，0：未检查，1：已检查
     * @param adUserName
     * @return
     */
    public int getStatus(String adUserName) {
        return studentDAO.getStatus(adUserName);
    }

    public boolean exists(String adUserName) {
        return studentDAO.exists(adUserName);
    }

    public boolean exists(String studentName, String studentNo) {
        return studentDAO.exists(studentName, studentNo);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean insertStudentInfo(String adUserName, String studentName, String studentNo, String sex, String xueYuan, int status) {
        try {
            studentDAO.insertStudentInfo(adUserName, studentName, studentNo, sex, xueYuan, status);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public JSONObject getStudentInfo(String adUserName) {
        return studentDAO.getStudentInfo(adUserName);
    }

    public String getSex(String adUserName) {
        return studentDAO.getSex(adUserName);
    }
}
